package com.yingsheng.bi.hks.template.processor;

import com.yingsheng.bi.hks.template.events.AdviceEvent;
import com.yingsheng.bi.hks.template.events.Event;

public class InnerProcessor implements Processor {

    private int order;

    @Override
    public int getOrder() {
        return this.order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    private Processor innerLogic;

    public Processor getInnerLogic() {
        return innerLogic;
    }

    public void setInnerLogic(Processor innerLogic) {
        this.innerLogic = innerLogic;
    }

    protected void execInner(Event event) {
        if (this.innerLogic != null) {
            this.innerLogic.exec(event);
        }
    }

    @Override
    public void exec(Event event) {
        if (event instanceof AdviceEvent) {
            AdviceEvent adviceEvent = (AdviceEvent) event;
            Object input = adviceEvent.getInput();
            try {
                execInner(event);
            } finally {
                adviceEvent.setInput(input);
            }
        } else {
            execInner(event);
        }
    }

}
